package com.flyxteam.supports.model;

/**
 * 分页逻辑模型，自动计划页码等
 * <ul>
 * <li>2017-05-06 | ChrisX | + create</li>
 * </ul>
 * @author ChrisX
 */
public class Paging {

    /** 当前页码，最小为1 */
	protected int pageNum;
	/** 每页的记录数，最小为1 */
	protected int pageSize;
	/** 总数据量，最小为0 */
    protected int totalRecords;

	/** 总页数 */
	protected int totalPages;
	/** 上一页页码 */
	protected int prevNum;
	/** 下一页页码 */
    protected int nextNum;
    /** 是否有上一页 */
    protected boolean hasPrev;
    /** 是否有下一页 */
    protected boolean hasNext;

    protected Paging() {}

	public Paging(int pageNum, int pageSize, int totalRecords) {
	    this.pageNum = pageNum;
	    this.pageSize = pageSize;
	    this.totalRecords = totalRecords;
	    compute();
	}

	/**
	 * 验证和计算页码信息
	 * @param pageNum
	 * @param pageSize
	 * @param totalRecords
	 */
	protected void compute() {
		// 验证总数据量
		if (totalRecords < 0) totalRecords = 0;
		// 验证每页记录数
		if (pageSize < 1) pageSize = 1;
		// 计算总页数
		totalPages = (int) totalRecords / pageSize;
		if (totalRecords % pageSize > 0) totalPages++;
		// 验证当前页数
		if (pageNum < 0) pageNum = 0;
		else if (pageNum > totalPages) pageNum = totalPages;
		// 计算是否有上一页
		hasPrev = (pageNum - 1 >= 1);
		prevNum = hasPrev ? pageNum - 1 : pageNum;
		// 计算是否有下一页
		hasNext = (pageNum + 1 <= totalPages);
		nextNum = hasNext ? pageNum + 1 : pageNum;
	}

	public String toString() {
        return this.getClass().getName() 
            + "["
            + "pageNum=" + pageNum
            + ", pageSize=" + pageSize 
            + ", totalRecords=" + totalRecords 
            + ", totalPages=" + totalPages 
            + ", prevNum=" + prevNum 
            + ", nextNum=" + nextNum 
            + ", hasPrev=" + hasPrev 
            + ", hasNext=" + hasNext 
            + "]";
    }

	/* property accessor */
	
    public int getPageNum() {
        return pageNum;
    }
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getTotalRecords() {
        return totalRecords;
    }
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }
    public int getTotalPages() {
        return totalPages;
    }
    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }
    public int getPrevNum() {
        return prevNum;
    }
    public void setPrevNum(int prevNum) {
        this.prevNum = prevNum;
    }
    public int getNextNum() {
        return nextNum;
    }
    public void setNextNum(int nextNum) {
        this.nextNum = nextNum;
    }
    public boolean isHasPrev() {
        return hasPrev;
    }
    public void setHasPrev(boolean hasPrev) {
        this.hasPrev = hasPrev;
    }
    public boolean isHasNext() {
        return hasNext;
    }
    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }
	
}
